package com.zqxq.file.exception;

import com.zqxq.common.utils.DateUtils;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Description ：BasicErrorController
 * @Author ： 解风
 * @Date ： 2017/09/26 19:57
 */
@Controller
public class MainsiteErrorController implements ErrorController {

    private static final String ERROR_PATH = "/error";

//    @RequestMapping(value=ERROR_PATH)
//    public String handleError(){
//        System.out.println("-----------------1");
//        return "error/error";
//    }

    @RequestMapping(value=ERROR_PATH)
    @ExceptionHandler(Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, HttpServletResponse response, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();

//        StringWriter sw = new StringWriter();
//        PrintWriter pw = new PrintWriter(sw, true);
//        pw.flush();
//        sw.flush();
        System.out.println("--------------------defaultErrorHandler-----------");
//        e.printStackTrace(pw);
        mav.addObject("timestamp", DateUtils.dateToStr(new Date(), DateUtils.DATE_FORMAT_DATETIME));
        mav.addObject("status", response.getStatus());
//        mav.addObject("exception", sw.toString());
        mav.addObject("url", req.getRequestURL());
        mav.setViewName("error/error");
        return mav;
    }

    @Override
    public String getErrorPath() {
        System.out.println("-----------------2");
        return  ERROR_PATH;
    }
}
